//
//  HUD.m
//  genePool
//
//  Created by Greg Dunn on 10/22/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "SSHUD.h"

#import "SSGame.h"
#import "SSGameScene.h"
#import "SSLevelSelectScene.h"
#import "SSMenuScene.h"
#import "SSStorage.h"
#import "SSGlobal.h"

@implementation SSHUD

#pragma mark init

- (id) initWithTitle:(NSString *)title haveScore:(BOOL)haveScore haveTime:(BOOL)haveTime haveHealth:(BOOL)haveHealth haveEnergy:(BOOL)haveEnergy
{
	self = [super init];
	
	if(self != nil)
	{
		self.isTouchEnabled = YES;
		self.isAccelerometerEnabled = YES;
		
		//Scene Label
		CCLabel *label = [CCLabel labelWithString:title fontName:@"Arial" fontSize:13];
		label.position = ccp(240, 300);
		[self addChild:label];
		
		//[MenuItemFont setFontName:@"Arial"];
		[CCMenuItemFont setFontSize:20];
	//	
//		CCMenuItem *menuItem1 = [CCMenuItemFont itemFromString:@"x" target:self selector:@selector(quitGame:)];
//		
//		CCMenu *menu = [CCMenu menuWithItems:menuItem1, nil];
//		[menu alignItemsVerticallyWithPadding:10];
//		menu.position = ccp(470,310);
//		[self addChild:menu];
		if (haveScore) {
			scoreBoard = [[SSHUDComponent alloc]initWithValue:[SSStorage getCurrentGameScore] andLabel:@"Score:"  andComponentFormat:@"%@ %06i" andFontName:@"Arial" andFontSize:12];
			scoreBoard.position = ccp(70, 300);					  
			[self addChild:scoreBoard];			
		}
		
		if (haveHealth) {
			healthBar = [[SSHUDComponent alloc]initWithValue:[SSStorage getCurrentHealth] andLabel:@"Health:"  andComponentFormat:@"%@ %03i" andFontName:@"Arial" andFontSize:12];
			//[[HUDBarComponent alloc]initWithValue:[Storage getCurrentHealth] andImage:@"Resources/hudBar2.png" andLabel:@"Health:" andComponentFormat:@"%@ %i" andFontName:@"Arial" andFontSize:12];
			healthBar.position = ccp(70, 277);					  
			[self addChild:healthBar];		
			//[[NSNotificationCenter defaultCenter] addObserver:healthBar selector:@selector(notificationValueChange:) name:@"PlayerHealthChange" object:nil]; 
		}

		if (haveEnergy) {
			energyBar = [[SSHUDComponent alloc]initWithValue:100 andLabel:@"Energy:"  andComponentFormat:@"%@ %03i" andFontName:@"Arial" andFontSize:12];
			//[[HUDBarComponent alloc]initWithValue:[Storage getCurrentHealth] andImage:@"Resources/hudBar2.png" andLabel:@"Energy:" andComponentFormat:@"%@ %i" andFontName:@"Arial" andFontSize:12];
			energyBar.position = ccp(70, 257);					  
			[self addChild:energyBar];	
		}
		
	//	pauseButton = [SSHUDButtonComponent buttonWithImage:@"pic_pause.png" atPosition:ccp(470, 300) target:self selector:@selector(pauseLevel:)];
		
		CCMenuItemToggle *itemPause = [CCMenuItemToggle itemWithTarget:self selector:@selector(pauseLevel:) items:
											 [CCMenuItemImage itemFromNormalImage:@"pic_pause.png" selectedImage:@"pic_pause.png"],
											 [CCMenuItemImage itemFromNormalImage:@"pic_pause.png" selectedImage:@"pic_pause.png"],
										nil];
		pauseButton = [CCMenu menuWithItems:itemPause, nil];
		pauseButton.position = ccp(460, 300);
		
		pauseButton.tag = 'pbtn';
		[self addChild:pauseButton];
		
		if (haveTime) {
			scoreTimer = [[SSHUDTimerComponent alloc]initWithValue:0 andLabel:@"Timer:"  andComponentFormat:@"%@ %03i" andFontName:@"Arial" andFontSize:12];
			scoreTimer.position = ccp(469, 253);					  
			scoreTimer.increment = 1;
			[self addChild:scoreTimer];
			[scoreTimer startTimer];
		}
		
		//optionsLayer = [[SSHUDOptionsLayer alloc] init];
		//[optionsLayer setVisible:NO];
//		[self addChild:optionsLayer];
	}	
	return self;
}


-(id)init{
	self = [super init];
	
	if(self != nil)
	{
		[self initWithTitle:@"Level 1" haveScore:YES haveTime:NO haveHealth:NO haveEnergy:NO];
	}
	return self;
}

-(int)getHealth
{
	return [healthBar componentValue];
}

-(int)updateHealth:(int)value
{
	return [healthBar updateValue:value];
}


-(int)removeHealth:(int)value
{
	return [healthBar updateValue: healthBar.componentValue - value];
}

-(void)addScore:(int)value
{
	[scoreBoard addValue:value];
}

- (void)quitGame:(id)sender
{
	//NSLog(@"on game");
	[[CCDirector sharedDirector] replaceScene:[SSMenuScene node]];
}

- (void)quitLevel
{
	//Goto Level Select Screen
	[[CCDirector sharedDirector] replaceScene:[SSLevelSelectScene node]];
};

- (void)restartLevel
{
	[[CCDirector sharedDirector] replaceScene:[SSGameScene node]];
};

-(void)pauseLevel:(id)sender{
	[[SSGlobal instance] clickButton];
	
	[pauseButton setVisible:NO];
	[[ [[self parent]parent] getChildByTag:'lJoy'] setVisible:NO];
	[[ [[self parent]parent] getChildByTag:'rBtn'] setVisible:NO];

	[[CCDirector sharedDirector] pause];
	SSOptionsLayer *optionsLayer2 = [SSOptionsLayer layerWithColor:ccc4(0, 0, 0, 125)];

	[self addChild:optionsLayer2];
	//[optionsLayer setVisible:YES];
}

- (void)pauseLevel2:(id)sender
{
	[[CCDirector sharedDirector] pause];
	[self removeChild:pauseButton cleanup:TRUE];
	pauseButton = [SSHUDButtonComponent buttonWithText:@"back" atPosition:ccp(480, 280) target:self selector:@selector(unpauseLevel:)];
	pauseButton.tag = 'pbtn';
	[self addChild:pauseButton];
};

- (void)unpauseLevel:(id)sender
{
	[[CCDirector sharedDirector] resume];
	[self removeChild:pauseButton cleanup:TRUE];
	pauseButton = [SSHUDButtonComponent buttonWithImage:@"label40x41.png" atPosition:ccp(460, 280) target:self selector:@selector(pauseLevel2:)];
	pauseButton.tag = 'pbtn';
	[self addChild:pauseButton];
};



//-(Vector3D *)getInputVector
//{
//	return [joyStick getCurrentVector];
//}
//
////Touch Controls
//-(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{	
//	
//	return [joyStick touchesBegan:touches withEvent:event];
//}
//
//-(BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
//{
//	//NSLog(@"ccTouchesMoved");
//	
//	return [joyStick touchesMoved:touches withEvent:event];
//}
//
//-(BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
//{
//	//NSLog(@"ccTouchesEnded");
//	
//	return [joyStick touchesEnded:touches withEvent:event];
//}

- (void)dealloc 
{
	[[NSNotificationCenter defaultCenter] removeObserver:healthBar];
	[super dealloc];
}



@end